let userTodaySort = document.getElementById("userTodaySort")
let userTomorrowSort = document.getElementById("userTomorrowSort")
let userTomorrowSortIcon= document.getElementById("userTomorrowSortIcon")
let userTodaySortIcon = document.getElementById("userTodaySortIcon");

userTodaySort.addEventListener("click",(event)=>{
    if (Cookies.get("todayTimeSort")=="OldToNew") {
        Cookies.set("todayTimeSort","NewToOld")
        userTodaySortIcon.setAttribute("class","glyphicon glyphicon-triangle-top")
        document.location.reload()
    }else  {
        Cookies.set("todayTimeSort","OldToNew")
        userTodaySortIcon.setAttribute("class","glyphicon glyphicon-triangle-bottom")
        document.location.reload()
    }


})
userTomorrowSort.addEventListener("click",(event)=>{

    if (Cookies.get("tomorrowTimeSort")=="OldToNew") {
        Cookies.set("tomorrowTimeSort","NewToOld")
        document.location.reload()

    }else  {
        Cookies.set("tomorrowTimeSort","OldToNew")
        document.location.reload()
    }

})

window.addEventListener("load", (event) => {
    if (Cookies.get("todayTimeSort")=="OldToNew") {
        userTodaySortIcon.setAttribute("class","glyphicon glyphicon-triangle-top")
    }else  {
        userTodaySortIcon.setAttribute("class","glyphicon glyphicon-triangle-bottom")
    }

    if (Cookies.get("tomorrowTimeSort")=="OldToNew") {
        userTomorrowSortIcon.setAttribute("class","glyphicon glyphicon-triangle-top")

    }else  {
        userTomorrowSortIcon.setAttribute("class","glyphicon glyphicon-triangle-bottom")
    }


    let todaydate = new Date()
    document.getElementById("todayDateTime").innerText = `Today Tasks:${todaydate.getMonth() + 1}|${todaydate.getDate()}|${todaydate.getFullYear()}`
    let tommorrowDate = new Date(todaydate.getTime()+86400000)
    document.getElementById("tommorrowDateTime").innerText = `Tomorrow Tasks:${tommorrowDate.getMonth() + 1}|${tommorrowDate.getDate()}|${tommorrowDate.getFullYear()}`

/*        .post('/selectTommorrowTasksEscTime',ManagerController.selectTommorrowTasksEscTime)
        .post('/selectTommorrowTaskDescTime',ManagerController.selectTommorrowTaskDescTime)*/
    if (Cookies.get("todayTimeSort")=="OldToNew") {
        url ="/manager/selectTodayTasksEscTime"
    }
    else if (Cookies.get("todayTimeSort")=="NewToOld"){
        url ="/manager/selectTodayTasksDescTime"
    }else {
        url = "/manager/selectTodayTasks"
    }
    $.post(url, null, (res) => {
        if (res.status == "3001") {
            alert(res.message)
            document.location = "/public/html/home.html"
        } else if (res.data) {


            let today_tasks_table = document.querySelector("#today_tasks_table>tbody");

            for (let resArrayElement of res.data) {
                let tr = document.createElement("tr")
                for (let objKeyAndValue in resArrayElement) {
                    let td = document.createElement("td")
                    if (objKeyAndValue == "due_date") {
                      let timeBigInt = resArrayElement[`${objKeyAndValue}`]*1000
                        let date= new Date(timeBigInt)
                        let dateMinutes = date.getMinutes().toString();
                        if(dateMinutes.length<2) {
                            dateMinutes=`0${dateMinutes}`
                        }
                        td.innerText =`${date.getHours()}:${dateMinutes}`
                    } else {
                        td.innerText = resArrayElement[`${objKeyAndValue}`]
                    }

                    tr.appendChild(td)

                }
                let tdOpration = document.createElement("td")
                let button = document.createElement("button")
                button.setAttribute("type","button")
                button.setAttribute("data-userid",resArrayElement.userid)
                button.setAttribute("data-taskid",resArrayElement.id)
                button.innerText="CANCEL"
                button.classList.add("button")
                button.classList.add("btn-warning")
                button.addEventListener("click",(event)=>{
                    let userid = event.target.getAttribute("data-userid")
                    let taskid = event.target.getAttribute("data-taskid")
                    $.post("/manager/cancelTaskOfUser", {"userid":userid,"taskid":taskid}, (res) => {
                        if (res.status == "3001") {
                            alert(res.message)
                            document.location = "/public/html/home.html"
                        } else  {
                            alert(res.message)
                            location.reload()

                        }

                    },"json")});
                tdOpration.appendChild(button)
                tr.appendChild(tdOpration)

                today_tasks_table.appendChild(tr)
            }
        } else {
            let today_tasks_table = document.querySelector("#today_tasks_table>tbody");
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = "No Today Tasks"
            tr.appendChild(td)
            today_tasks_table.appendChild(tr)

        }

    }, "json")
    let tomorrowUrl
    if (Cookies.get("tomorrowTimeSort")=="OldToNew") {
        tomorrowUrl ="/manager/selectTomorrowTasksEscTime"
    }
    else if (Cookies.get("tomorrowTimeSort")=="NewToOld"){
        tomorrowUrl ="/manager/selectTomorrowTasksDescTime"
    }else {
        tomorrowUrl = "/manager/selectTommorrowTasks"
    }

    $.post(tomorrowUrl, null, (res) => {


   if (res.data) {


            let tomorrow_tasks_table = document.querySelector("#tomorrow_tasks_table>tbody");

            for (let resArrayElement of res.data) {
                let tr = document.createElement("tr")
                for (let objKeyAndValue in resArrayElement) {
                    let td = document.createElement("td")
                    if (objKeyAndValue == "due_date") {
                        let timeBigInt = resArrayElement[`${objKeyAndValue}`]*1000
                        let date= new Date(timeBigInt)
                        let dateMinutes = date.getMinutes().toString();
                        if(dateMinutes.length<2) {
                            dateMinutes=`0${dateMinutes}`
                        }
                        td.innerText =`${date.getHours()}:${dateMinutes}`
                    } else {
                        td.innerText = resArrayElement[`${objKeyAndValue}`]
                    }
                    tr.appendChild(td)
                }
                tomorrow_tasks_table.appendChild(tr)
            }
        } else {
            let tomorrow_tasks_table = document.querySelector("#tomorrow_tasks_table>tbody");
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = "No Tomorrow Tasks"
            tr.appendChild(td)
            tomorrow_tasks_table.appendChild(tr)

        }


    })
})


